test_description="quarantine-helper scan"

. ./lib/sharness.sh

Quaranine="*incoming*"

test_expect_success "setup" '
	git init --bare remote.git &&
	cp "$HOME/../resources/pre-receive" remote.git/hooks/
'

test_expect_success "skip when committing from web" '
	(
		cd remote.git &&
		git config transfer.unpacklimit 10 &&
		quarantine-helper scan -w
	) >actual &&
	test -f actual && test ! -s actual
'

test_expect_failure "scan quarantine(one commit, loose object), not set repo path" '
	git clone --no-local remote.git local &&
	(
		cd local &&
		generate_commits 0 0 &&
		git push
	) &&
	cat >expect<<-EOF &&
	6201bff8206a22207277d0e64bac92ec01f5cb10
	EOF
	quarantine-helper scan -q $Quaranine >actual &&
	test_cmp expect actual
'

test_expect_success "clean workspace" '
	rm -rf remote.git &&
	rm -rf local &&
	git init --bare remote.git &&
	cp "$HOME/../resources/pre-receive" remote.git/hooks/ 
'



test_expect_success "scan quarantine(one commit, loose object), set repo path as cml parameter" '
	git clone --no-local remote.git local &&
	(
		cd local &&
		generate_commits 0 0 &&
		git push
	) &&
	cat >expect<<-EOF &&
	6201bff8206a22207277d0e64bac92ec01f5cb10
	EOF
	quarantine-helper scan -r local/.git -q $Quaranine >actual &&
	test_cmp expect actual
'

test_expect_success "clean workspace" '
	rm -rf remote.git &&
	rm -rf local &&
	git init --bare remote.git &&
	cp "$HOME/../resources/pre-receive" remote.git/hooks/ 
'

test_expect_success "scan quarantine(one commit, loose object), set repo path as env" '
	git clone --no-local remote.git local &&
	(
		cd local &&
		generate_commits 0 0 &&
		git push
	) &&
	cat >expect<<-EOF &&
	6201bff8206a22207277d0e64bac92ec01f5cb10
	EOF
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine >actual &&
	test_cmp expect actual
'


test_expect_success "scan quarantine(5 commits, packfile)" '
	rm -rf $Quaranine &&
	(
		cd local &&
		generate_commits 1 5 &&
		git push
	)
	cat >expect<<-EOF &&
	042f968d17d76f896591fe05454208ecd64291a5
	1b92441c9aeca6a9b4b1df3f24e2ecbea7ec4096
	4272e5f09914017057bc328da23de529a23dc946
	54a2d8b0c097f71e3e43c046ad2beb51bbbf2955
	c70a396ef27abad5199144f6287a3b891e097c11
	EOF
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine >actual &&
	test_cmp expect actual
'

test_expect_success "new branch with no new commits" '
	rm -rf $Quaranine &&
	(
		cd local &&
		git checkout -b topic1&&
		git push -u origin topic1
	) &&
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine >actual &&
	test -f actual && test ! -s actual
'

test_expect_success "push multiple branch at the same time" '
	rm -rf $Quaranine &&
	(
		cd local &&
		git checkout -b fix0  master &&
		git checkout -b fix1  master &&
		generate_commits 30 32 &&
		git checkout -b fix2  master &&
		generate_commits 33 34 &&
		git checkout -b fix3  master &&
		generate_commits 35 35 &&
		git push --all
	) &&
	cat >expect<<-EOF &&
	19ee49f0b2d4f0dd13d9fdf7257f022af8a9687b
	3998b99b8b99fd5936f9c57c9d7d2b51ef880537
	48c16d6042cd71984f0e33d1b40b6bc39c87398f
	c86119e692ca7db5d63747ce2042730df1cd6bab
	d149052326d311e6dd2c449f536f30ab13b2109c
	e4d1b749701d7e5f47df479ebdd752ae11310044
	EOF
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine >actual &&
	test_cmp expect actual
'

test_expect_success "new branch with new commits" '
	rm -rf $Quaranine &&
	(
		cd local &&
		git checkout -b topic2 master&&
		generate_commits 26 28 &&
		git push -u origin topic2
	) &&
	cat >expect<<-EOF &&
	10ae3395d7419f9ffea89f165f49ed1e4fdb9da2
	21f2933c8f214c99eae6f38b8c8bb9c3fe266dff
	cfb0f5202fe5dcebf53322709266c54d4bb4abca
	EOF
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine >actual &&
	test_cmp expect actual
'

test_expect_success "force push, roll-back, no commits in quarantine" '
	rm -rf $Quaranine &&
	(
		cd local &&
		git reset --hard  10ae3395d7419f9ffea89f165f49ed1e4fdb9da2 &&
		git push -f -u origin topic2
	) &&
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine >actual &&
	test -f actual && test ! -s actual
'

test_expect_success "merge" '
	rm -rf $Quaranine &&
	(
		cd local &&
		git checkout -b topic3  master &&
		generate_commits 29 29 &&
		git merge topic2 &&
		git push -f -u origin topic3
	) &&
	cat >expect<<-EOF &&
	03e1a5508ebe2c38e75d03b8f093880a1978033f
	0fa6d1a6ff6794110845d28b1990afe27d11d703
	EOF
	GIT_DIR=local/.git quarantine-helper scan -q $Quaranine >actual &&
	test_cmp expect actual
'
test_done

